package com.huangxunyi.GUET;

/**
 * //=======================================================
 * //		          .----.
 * //		       _.'__    `.
 * //		   .--(^)(^^)---/!\
 * //		 .' @          /!!!\
 * //		 :         ,    !!!!
 * //		  `-..__.-' _.-\!!!/
 * //		        `;_:    `"'
 * //		      .'"""""`.
 * //		     /,  ya ,\\
 * //		    //狗神保佑\\
 * //		    `-._______.-'
 * //		    ___`. | .'___
 * //		   (______|______)
 * //=======================================================
 * Created by huang on 2017/2/25.
 */
public class RefreshCmd {
    private final static byte REFRESH_CMD_REFRESH_ONLINE = 0x1e;
    private final static byte REFRESH_CMD_REFRESH_ONLINE_REPLY = 0x1f;
    private final static byte REFRESH_CMD_REFRESH_0A = 0x0a;
    private final static byte REFRESH_CMD_REFRESH_0A_REPLY = 0x0b;
    private final static byte REFRESH_CMD_REFRESH_GET_NEWS = 0x05;
    private final static byte REFRESH_CMD_REFRESH_GET_NEWS_REPLY = 0x06;

    private final static int REFRESH_PACKET_LENGTH = 500;
    private final static int REFRESH_PACKET_FILL_BYTE = 0xff;
    private final static int REFRESH_PACKET_RANDOM_STRING_LENGTH = 0x29;

    private final static String COPYRIGHT_VERSION = "@Version: 1.85, GUET";

    private String userName = "";
    private int refreshKey = 0;

    public RefreshCmd(String userName, int refreshKey) {
        this.userName = userName;
        this.refreshKey = refreshKey + 1500;
    }

    public void setRefrshKey(int refreshKey) {
        this.refreshKey = refreshKey + 1500;
    }

    private static byte[] packRefreshPacket(RefreshPacketModel model) {
        byte[] re = null;
        try {
            re = new byte[REFRESH_PACKET_LENGTH];
            int index = 0;
            for (byte b : IPClientUtils.short2byte(model.getBanner())) {
                re[index++] = b;
            }
            re[index++] = model.getCommand();
            for (byte b : IPClientUtils.long2byte(model.getKey())) {
                re[index++] = b;
            }
            for (byte b : IPClientUtils.long2byte((long) model.getFlow())) {
                re[index++] = b;
            }
            for (byte b : IPClientUtils.long2byte((long) model.getMoney())) {
                re[index++] = b;
            }
            for (byte b : IPClientUtils.int2byte(model.getLen1())) {
                re[index++] = b;
            }

            for (byte b : model.getStr1().getBytes("gbk")) {
                re[index++] = b;
            }
            for (byte b : IPClientUtils.int2byte(model.getLen2())) {
                re[index++] = b;
            }
            for (byte b : model.getStr2().getBytes("gbk")) {
                re[index++] = b;
            }
            for (byte b : IPClientUtils.int2byte(model.getLen3())) {
                re[index++] = b;
            }
            for (byte b : model.getMsg().getBytes("gbk")) {
                re[index++] = b;
            }
            for (byte b : IPClientUtils.int2byte(model.getLen4())) {
                re[index++] = b;
            }
            for (byte b : model.getStr4().getBytes("gbk")) {
                re[index++] = b;
            }
            for (int i = index; i < re.length; i++) {
                re[i] = (byte) REFRESH_PACKET_FILL_BYTE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return re;
    }

    private static RefreshPacketModel unpackRefreshPacket(byte[] packet) {
        RefreshPacketModel model = null;
        try {
            if (packet != null && ((int) packet[0] & 0xff) == 0x82
                    && ((int) packet[1] & 0xff) == 0x23) {
                model = new RefreshPacketModel();

                model.setCommand(packet[2]);
                int index = 3;

                model.setKey(IPClientUtils.byte2long(packet, index));
                index += 8;

                model.setFlow(IPClientUtils.byte2double(packet, index));
                index += 8;

                model.setMoney(IPClientUtils.byte2double(packet, index));
                index += 8;

                model.setLen1(IPClientUtils.byte2int(packet, index));
                index += 4;
                model.setStr1(new String(packet, index, model.getLen1(), "gbk"));
                index += model.getLen1();

                model.setLen2(IPClientUtils.byte2int(packet, index));
                index += 4;
                model.setStr2(new String(packet, index, model.getLen2(), "gbk"));
                index += model.getLen2();

                model.setLen3(IPClientUtils.byte2int(packet, index));
                index += 4;
                model.setMsg(new String(packet, index, model.getLen3(), "gbk"));
                index += model.getLen3();

                model.setLen4(IPClientUtils.byte2int(packet, index));
                index += 4;
                model.setStr4(new String(packet, index, model.getLen4(), "gbk"));

            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return model;
    }

    public static byte[] getRefreshGetNewsPacket() {
        return packRefreshPacket(new RefreshPacketModel(
                REFRESH_CMD_REFRESH_GET_NEWS,//
                IPClientUtils.getRandomInt(888888, 111111),//
                IPClientUtils.getRandomInt(888888, 111111) << 32,//
                IPClientUtils.getRandomInt(888888, 111111) << 32,//
                "a", //
                "b", //
                IPClientUtils
                        .getRandomString(REFRESH_PACKET_RANDOM_STRING_LENGTH), //
                COPYRIGHT_VERSION));
    }

    public static RefreshPacketModel checkRefreshGetNewsReply(byte[] packet) {
        RefreshPacketModel model = unpackRefreshPacket(packet);
        if (model != null
                && model.getCommand() == REFRESH_CMD_REFRESH_GET_NEWS_REPLY) {
            return model;
        }
        return null;
    }

    public byte[] getRefresh0APacket() {
        return packRefreshPacket(new RefreshPacketModel(REFRESH_CMD_REFRESH_0A,//
                IPClientUtils.getRandomInt(888888, 111111),//
                IPClientUtils.getRandomInt(888888, 111111) << 32,//
                IPClientUtils.getRandomInt(888888, 111111) << 32,//
                "a", //
                "b", //
                IPClientUtils
                        .getRandomString(REFRESH_PACKET_RANDOM_STRING_LENGTH), //
                COPYRIGHT_VERSION));
    }

    public boolean checkRefresh0AReply(byte[] packet) {
        RefreshPacketModel model = unpackRefreshPacket(packet);
        return model != null && model.getCommand() == REFRESH_CMD_REFRESH_0A_REPLY;
    }

    public byte[] getRefreshOnlinePacket() {
        return packRefreshPacket(new RefreshPacketModel(
                REFRESH_CMD_REFRESH_ONLINE,//
                refreshKey,//
                (long) IPClientUtils.getRandomInt(888888, 111111) << 32,//
                (long) IPClientUtils.getRandomInt(888888, 111111) << 32,//
                userName, //
                "b", //
                "今天天气真好", //
                COPYRIGHT_VERSION));
    }

    public RefreshPacketModel checkRefreshOnlineReply(byte[] packet) {
        RefreshPacketModel model = unpackRefreshPacket(packet);
        if (model != null
                && model.getCommand() == REFRESH_CMD_REFRESH_ONLINE_REPLY) {
            return model;
        }
        return null;
    }
}
